package com.example.springboot.controller;


import com.example.springboot.pojo.Img;
import com.example.springboot.pojo.Result;
import com.example.springboot.service.FileService;
import com.example.springboot.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/home")
@CrossOrigin(origins = "*")
public class HomeController {
    @Autowired
    private HomeService homeService;
    @Autowired
    private FileService fileService;

    @PostMapping("/uploadImg")
    public Result<?> uploadImg(MultipartFile file) throws IOException {
        String url = fileService.uploadFile(file);
        homeService.uploadImg(url);
        return Result.success("上传成功");
    }

    @GetMapping("/getAllImg")
    public Result<?> getAllImg(){
        Img[] images = homeService.getAllImg();
        return Result.success(images);
    }

    @PutMapping("/deleteImg")
    public Result<?> deleteImg(Integer id){
        String url = homeService.findById(id);
        homeService.deleteImg(id);
        fileService.deleteFile(url);
        return Result.success();
    }
}
